package org.example.system.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.entity.Remark;
import org.example.system.entity.OperateLog;
import org.example.system.service.RemarkService;
import org.example.system.service.OperateLogService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 响应控制层
 */
@RestController
@RequestMapping("/admin/remark")
@Slf4j
public class RemarkController {
    @Autowired
    private RemarkService remarkService;
    @Autowired
    private OperateLogService operateLogService;

    //操作日志对象
    private OperateLog operateLog = new OperateLog("求助评论管理");

    /**
     * 按条件分页查询
     * @param remark
     * @return
     */
    @GetMapping("/list")
    public R list(Remark remark){
        IPage<Remark> page = remarkService.selectPage(remark);
        log.info("------------ RemarkController | list | selectPage | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 添加响应
     * @param remark
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Remark remark, HttpServletRequest request){
        operateLog.setUsername(remark.getCreateBy());
        log.info("--------- RemarkController | add | input : {}", JSON.toJSON(remark));
        int row = remarkService.add(remark);
        if(row <= 0){
            operateLog.setInfo("添加失败");
            operateLogService.add(operateLog,request);
            return R.fail("添加失败");
        }
        operateLog.setInfo("添加成功");
        operateLogService.add(operateLog,request);
        return R.ok("添加成功");
    }

    /**
     * 修改
     * @param remark
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Remark remark,HttpServletRequest request){
        operateLog.setUsername(remark.getUpdateBy());
        log.info("--------- RemarkController | update | input : {}", JSON.toJSON(remark));
        if(null == remark.getId() || remark.getId() <= 0){
            operateLog.setInfo("参数错误:ID未传入");
            operateLogService.add(operateLog,request);
            return R.fail("参数错误:ID未传入");
        }
        int row = remarkService.update(remark);
        if(row <= 0){
            operateLog.setInfo("修改失败");
            operateLogService.add(operateLog,request);
            return  R.fail("修改失败");
        }
        operateLog.setInfo("修改成功");
        operateLogService.add(operateLog,request);
        return R.ok("修改成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping( "/delete")
    public R delete(Long[] ids,String sender,HttpServletRequest request){
        operateLog.setUsername(sender);
        log.info("--------- RemarkController | delete | input : {}", JSON.toJSON(ids));
        int row = remarkService.deleteBatch(ids);
        if(row <= 0){
            operateLog.setInfo("删除失败");
            operateLogService.add(operateLog,request);
            return R.fail("删除失败");
        }
        operateLog.setInfo("删除成功");
        operateLogService.add(operateLog,request);
        return R.ok("删除成功");
    }

    /**
     * 根据id查询响应数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable("id") Long id){
        log.info("--------- RemarkController | getInfo | input : {}", JSON.toJSON(id));
        return R.ok(remarkService.getOne(id));
    }
}
